package com.nec.girl.controller;

import com.nec.girl.domain.Person;
import com.nec.girl.inter.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.List;

@RestController
@RequestMapping("/person")
public class HelloController {

    @Autowired
    private PersonRepository repository;

    @Value("spring.datasource.url")
    private String url;

    @GetMapping("/all")
    public List<Person> getAllPersons() {
        return repository.findAll();
    }

    @GetMapping("/{old_age}/{new_age}")
    public List<Person> getPersonAgeBetween(@PathVariable("old_age") String old_age,
                                            @PathVariable("new_age") String new_age) {
        return repository.findByAgeBetween(old_age, new_age);
    }

    @PostMapping("/saveOne")
    @Transactional
    public Person saveOnePerson(@RequestBody Person person)
    {
        System.out.println(person);
        return repository.save(person);
    }
}
